import java.io.*;
import java.util.Scanner;

import static java.lang.System.*;

public class test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(in);

        // 读取模板文件
        String templateFileName = "wuliu.template";
        String templateContent = readFile(templateFileName);

        if (templateContent == null) {
            out.println("模板文件不存在或读取错误。");
            return;
        }

        // 获取用户输入的信息
        out.println("请输入运单号：");
        String danhao = scanner.nextLine();
        out.println("请输入快件到达位置：");
        String weizhi = scanner.nextLine();
        out.println("请输入提取码：");
        String tiquma = scanner.nextLine();

        // 替换模板中的内容
        String replacedContent = templateContent.replace("{danhao}", danhao)
                .replace("{weizhi}", weizhi)
                .replace("{tiquma}", tiquma);

        // 写入到物流.txt文件中
        boolean result = writeFile("物流.txt", replacedContent);

        if (result) {
            out.println("物流信息已写入到物流.txt文件中。");
        } else {
            out.println("写入文件失败。");
        }

        scanner.close();
    }

    private static String readFile(String fileName) {
        StringBuilder contentBuilder = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
            String currentLine;
            while ((currentLine = reader.readLine()) != null) {
                contentBuilder.append(currentLine).append(lineSeparator());
            }
        } catch (IOException e) {
            out.println("读取文件时发生错误：" + e.getMessage());
            return null;
        }
        return contentBuilder.toString();
    }

    private static boolean writeFile(String fileName, String content) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
            writer.write(content);
        } catch (IOException e) {
            out.println("写入文件时发生错误：" + e.getMessage());
            return false;
        }
        return true;
    }
}
